#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
int Ll(const char * path)
{

	DIR* dir = NULL;
	if(NULL == path)
	{
		dir = opendir(".");
	}
	else
	{
		dir = opendir(path);
	}
	if(NULL == dir)
	{
		perror("opendir");
		return 1;
	}
	while(1)
	{
		struct dirent * fo = readdir(dir);
		if(NULL == fo)
		{
			break;
		}
		struct stat buf;
		bzero(&buf,sizeof(buf));
		char path1[20] = {0};
		int ret = 0;
		if(NULL != path)
		{
			sprintf(path1,"%s/%s",path,fo->d_name);
			ret = stat(path1,&buf);
		}
		else
		{
			ret = stat(fo->d_name,&buf);
		}
		if(-1 == ret)
		{
			perror("stat");
			return 1;
		}

		if(S_ISREG(buf.st_mode))
		{
			fputc('-',stdout);
		}
		else if(S_ISDIR(buf.st_mode))
		{
			fputc('d',stdout);
		}
		else if(S_ISCHR(buf.st_mode))
		{
			fputc('c',stdout);
		}
		else if(S_ISBLK(buf.st_mode))
		{
			fputc('b',stdout);
		}
		else if(S_ISFIFO(buf.st_mode))
		{
			fputc('p',stdout);
		}
		else if(S_ISLNK(buf.st_mode))
		{
			fputc('l',stdout);
		}
		else if(S_ISSOCK(buf.st_mode))
		{
			fputc('s',stdout);
		}




		if(buf.st_mode & S_IRUSR)
		{
			fputc('r',stdout);
		}
		else
		{
			fputc('-',stdout);
		}
		if(buf.st_mode & S_IWUSR)
		{
			fputc('w',stdout);
		}
		else
		{
			fputc('-',stdout);
		}

		if(buf.st_mode & S_IXUSR)
		{
			fputc('x',stdout);
		}
		else
		{
			fputc('-',stdout);
		}


		if(buf.st_mode & S_IRGRP)
		{
			fputc('r',stdout);
		}
		else
		{
			fputc('-',stdout);
		}
		if(buf.st_mode & S_IWGRP)
		{
			fputc('w',stdout);
		}
		else
		{
			fputc('-',stdout);
		}

		if(buf.st_mode & S_IXGRP)
		{
			fputc('x',stdout);
		}
		else
		{
			fputc('-',stdout);
		}

		if(buf.st_mode & S_IROTH)
		{
			fputc('r',stdout);
		}
		else
		{
			fputc('-',stdout);
		}
		if(buf.st_mode & S_IWOTH)
		{
			fputc('w',stdout);
		}
		else
		{
			fputc('-',stdout);
		}

		if(buf.st_mode & S_IXOTH)
	 	{
			fputc('x',stdout);
		}
		else
		{
			fputc('-',stdout);
		}



		time_t tm = buf.st_mtime;
		struct tm * info = localtime(&tm);
		struct passwd * pw = getpwuid(buf.st_uid);
		struct group * gr = getgrgid(buf.st_gid);
		printf(" %2d %s %s %6lu %d-%02d-%02d %02d:%02d:%02d  %s\n",buf.st_nlink,pw->pw_name,gr->gr_name
				,buf.st_size,info->tm_year+1900,info->tm_mon+1,info->tm_mday
				,info->tm_hour,info->tm_min,info->tm_sec,fo->d_name);
	}
	closedir(dir);
	return 0;
}

